package com.itheima.day13.Zuoye.Zuoye1;

import org.dom4j.util.StringUtils;

public class Gift {
    private int x;
    private int xiaowangCount = 0;  // 小王发送计数
    private int xiaohongCount = 0;  // 小红发送计数

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setXiaowangCount(int xiaowangCount) {
        this.xiaowangCount = xiaowangCount;
    }

    public void setXiaohongCount(int xiaohongCount) {
        this.xiaohongCount = xiaohongCount;
    }

    public Gift(int x) {
        this.x = x;
    }


    public void send()
    {
        while (x<=100&&x>10) {
            Thread thread1 = Thread.currentThread();
                System.out.println(thread1.getName()+"正在发送第"+x+"个礼物");
                this.x--;
            // 使用同步块确保线程安全
            synchronized (this) {
                if ("小王".equals(thread1.getName())) {
                    xiaowangCount++;
                    System.out.println(thread1.getName() + "已发送" + xiaowangCount + "个礼物");
                } else if ("小红".equals(thread1.getName())) {
                    xiaohongCount++;
                    System.out.println(thread1.getName() + "已发送" + xiaohongCount + "个礼物");
                }
            }
        }
        System.out.println("礼物已经发放完毕");
    }
    // 添加获取统计结果的方法
    public int getXiaowangCount() {
        return xiaowangCount;
    }

    public int getXiaohongCount() {
        return xiaohongCount;
    }


}
